#ifndef _DSWLIVEVIDEO_H
#define _DSWLIVEVIDEO_H
#include <libbase/dswbase.h>
#include "ft2d_gfx.h"

class CDswlivevideo;

#define MAX_OSDFONT 256

typedef struct{
	bool bValid;
	FT2D_GfxDevice gfx_device;
	FT2D_GraphicsDeviceFuncs* gfx_funcs;
	FT2D_GfxDrv_Data* drv_data;
	FT2D_Surface_Data* tar_data;
	FT2D_GFX_Setting_Data* accel_data;
	
	gm_dim_t nScreen;
	gm_rect_t* nLocalScreen[MAX_CHANNEL_LOCAL];
	gm_rect_t* nRemoteScreen[MAX_CHANNEL_REMOTE];

	void* groupfd;
	void* bindfd_local[MAX_CHANNEL_LOCAL];
	void* bindfd_remote[MAX_CHANNEL_REMOTE];
	void* cap_obj_local[MAX_CHANNEL_LOCAL];
	void* file_obj_remote[MAX_CHANNEL_REMOTE];
	void* win_obj_local[MAX_CHANNEL_LOCAL];
	void* win_obj_remote[MAX_CHANNEL_REMOTE];

	CDswlivevideo* pCaller;

	unsigned char nFontBmp[MAX_CHANNEL_LOCAL][MAX_OSDFONT * 16 * 16 * 2];
	int nFontCount[MAX_CHANNEL_LOCAL];
	unsigned char nDigitalFontBmp[MAX_OSDFONT * 16 * 16 * 2];
	time_t nLastTm;

	ST_TIMER_PARAM_T nOsdTask;

	ST_SIGNAL_T nSignal;
} ST_SCREEN_INFO_T, *LPST_SCREEN_INFO_T;

class CDswlivevideo : public CDswbase
{
public:
	CDswlivevideo(LPST_ENV_T pEnv);
	virtual ~CDswlivevideo();
public:
	void SetVideoSignal(int nCh, bool bLocal, EN_VIDEO_SIGNAL nType);
private:
	void InitLiveView(LPST_SCREEN_INFO_T pScreenInfo, gm_rect_t *pLocalScreen, gm_rect_t *pRemoteScreen);
	void UnInitLiveView(LPST_SCREEN_INFO_T pScreenInfo);
	void Setup_win_attr(gm_rect_t *pScreen, gm_win_attr_t *win_attr, int ch, int visible, int fullscreen, gm_dim_t *pSize);
	void ClearWin(gm_rect_t *pScreen, int fullscreen, gm_dim_t *pSize);

	static void* cbPlaybackThreadFunc(void* pParam){
		LPST_SCREEN_INFO_T pScreenInfo = (LPST_SCREEN_INFO_T)pParam;
		pScreenInfo->pCaller->PlaybackThreadFunc(pScreenInfo);
		return NULL;
	};
	void PlaybackThreadFunc(LPST_SCREEN_INFO_T pScreenInfo);

	void DrawRect(LPST_SCREEN_INFO_T pScreenInfo, FT2DRectangle* pRect);

	void MakeOsdFont(LPST_SCREEN_INFO_T pScreenInfo, LPST_VIDEO_PARAM pVParam);
	int ConvertChar2WChar(char* pIn, wchar_t* pOut);
	int LoadFont(FILE* fp, wchar_t* pText, int ncount, unsigned char* pDot);
	void ConvertDot2Bmp565(unsigned char* pDot, int nlen, unsigned char* pBmp);

	void DrawTxtOsd(LPST_SCREEN_INFO_T pScreenInfo);
	bool DrawTmOsd(LPST_SCREEN_INFO_T pScreenInfo, time_t nNow);
	unsigned int think2d_modesize(unsigned int mode);

	static void cbOsdTask(int fd, short events, void* ctx){(((LPST_SCREEN_INFO_T)ctx)->pCaller)->OsdTask(fd, events, (LPST_SCREEN_INFO_T)ctx);};
	void OsdTask(int fd, short events, LPST_SCREEN_INFO_T pScreenInfo);
	
	static void cbLiveNotify(gm_obj_type_t obj_type, int vch, gm_notify_t notify);

	void InitSignal(LPST_SIGNAL_T pSignal);
	void UnInitSignal(LPST_SIGNAL_T pSignal);
	void DrawVideoSignal(LPST_SIGNAL_T pSignal, LPST_SCREEN_INFO_T pScreenInfo);
	void DrawPic(bool bDraw, gm_rect_t* pScreen, LPST_SCREEN_INFO_T pScreenInfo);

	void PlaybackDelay(unsigned int nInterval, int nFps);
private:
	ST_SCREEN_INFO_T m_nScreenInfo;
	gm_rect_t m_nLocalScreen[MAX_CHANNEL_LOCAL], m_nRemoteScreen[MAX_CHANNEL_REMOTE];
	pthread_t m_nThreadID;
	unsigned char* m_pClrwinBuf;
};

#endif

